package logic;

public class Tools {
	
	public static Hand simpleHigher(Hand h1, Hand h2, int pos) {
		if (pos < 0) {
			return null;
		}
		
		if(h1.pontos(pos) == h2.pontos(pos)) {
			return simpleHigher(h1,h2,pos-1);
		}else {
			return (h1.pontos(pos) > h2.pontos(pos))? h1 : h2 ;
		}
	}

	public static Hand bestPair(Hand h1, Hand h2) {
		if(h1.getPair()!= h2.getPair()) {
			return (h1.getPair()>h2.getPair())?h1:h2;
		}
		
		if(h1.getGame() == PokerGames.TWOPAIR) {
			if(h1.getSecondPair()!= h2.getSecondPair()) {
				return (h1.getSecondPair()>h2.getSecondPair())?h1:h2;
			}
		}
		
		return simpleHigher(h1,h2,4);
	}

	public static Hand bestThree(Hand h1, Hand h2) {
		if(h1.getThree() != h2.getThree()) {			
			return (h1.getThree() > h2.getThree())?h1:h2;
		}
		
		return simpleHigher(h1,h2,4);
	}

	public static Hand bestStraight(Hand h1, Hand h2) {
		if(h1.pontos(4)!=h2.pontos(4)) {
			return (h1.pontos(4)>h2.pontos(4))?h1:h2;
		}
		return null;
	}

	public static Hand bestFlush(Hand h1, Hand h2) {
		return simpleHigher(h1,h2,4);
	}

	public static Hand bestStraightFlush(Hand h1, Hand h2) {		
		return bestStraight(h1,h2);
	}

	public static Hand bestGame(Hand h1, Hand h2) {
		int comparisson = h1.getGame().compareTo(h2.getGame());
		
		if( comparisson != 0) {
			if(comparisson < 0) {
				return h2;		
			}
			return h1;
		}else {
			PokerGames game = h1.getGame();
			if(game.compareTo(PokerGames.SIMPLE) == 0) {
				return simpleHigher(h1,h2,4);
			}else if((game.compareTo(PokerGames.PAIR) == 0)||(game.compareTo(PokerGames.TWOPAIR) == 0)) {
				return bestPair(h1,h2);
			}else if(game.compareTo(PokerGames.THREEOFAKIND) == 0) {
				return bestThree(h1, h2);
			}else if(game.compareTo(PokerGames.STRAIGHT) == 0) {
				return bestStraight(h1, h2);
			}else if(game.compareTo(PokerGames.FLUSH) == 0) {
				return bestFlush(h1, h2);
			}else if(game.compareTo(PokerGames.FULL) == 0) {
				return bestFull(h1, h2);
			}else if(game.compareTo(PokerGames.FOUROFAKIND) == 0) {
				return bestThree(h1, h2);
			}else if(game.compareTo(PokerGames.STRAIGHTFLUSH) == 0) {
				return bestStraightFlush(h1, h2);
			}
		}
		
		return null;
	}

	private static Hand bestFull(Hand h1, Hand h2) {
		if(h1.getThree() != h2.getThree()) {			
			return (h1.getThree() > h2.getThree())?h1:h2;
		}
		
		if(h1.getPair() != h2.getPair()) {			
			return (h1.getPair() > h2.getPair())?h1:h2;
		}
		
		return null;
	}

}
